package com.weasel.weixin.mp;

import com.weasel.weixin.commons.CommonResponse;
import com.weasel.weixin.commons.user.UserGroupListResponse;
import com.weasel.weixin.commons.user.UserGroupRequest;
import com.weasel.weixin.commons.user.UserGroupResponse;
import com.weasel.weixin.commons.user.bean.Group;
import com.weasel.weixin.mp.builder.MpConfigBuilder;
import com.weasel.weixin.mp.config.MpConfig;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**accessToken测试
 * 按顺序运行main方法来测试各方法
 * Created by Dylan on 2015/9/28.
 */
public class UserGroupDemo {

    public static MpConfig config = null;
    public static UserGroupRequest request = null;
    static {

        try {
            config = MpConfigBuilder.fromXML("classpath:weixin-config.xml");
            request = new UserGroupRequest(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  static void main(String [] agrs) throws IOException, ExecutionException {

        //create();
        //update();
        list();
        //moveUserToOtherGroup();
        //batchMoveUserToOtherGroup();
        //delete();
        //list();

    }

    public static void create() throws IOException, ExecutionException {
        UserGroupResponse response = request.create("分组一");
        System.out.print(response);
    }

    public static void update() throws IOException, ExecutionException {

        Group group = new Group();
        group.setId(100);
        group.setName("分组二");
        UserGroupResponse response = request.update(group);
        System.out.print(response);
    }

    public static void delete() throws IOException, ExecutionException {

        CommonResponse _response = request.delete(100);
        System.out.print(_response);
    }

    public static void moveUserToOtherGroup() throws IOException, ExecutionException {

        String openId = "oK8blwRGdeggPAKa-k3SoTym57Oc";

        CommonResponse response = request.moveUser(openId, 100);
        System.out.print(response);

        int groupId = request.getUserGroupId(openId);

        System.out.print("new groupId : " + groupId);
    }

    public static void batchMoveUserToOtherGroup() throws IOException, ExecutionException {

        String openId1 = "oK8blwf6ve4ydC4vzH7ENt0UKgI4";
        String openId2 = "oK8blwRGdeggPAKa-k3SoTym57Oc";

        CommonResponse response = request.batchMoveUser(new String[]{openId1, openId2}, 100);
        System.out.print(response);

        int groupId1 = request.getUserGroupId(openId1);

        int groupId2 = request.getUserGroupId(openId2);

        System.out.print("new groupId : [" + groupId1+","+groupId2+"]");
    }

    public static void list() throws IOException, ExecutionException {

        UserGroupListResponse response = request.getList();
        System.out.print(response);
    }
}
